<template>
	<div class="app-container">
		<el-container>
			<el-main>
				<div>
					<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
						label-width="68px">
						<el-form-item label="学校">
							<el-select v-model="queryParams.schoolid" placeholder="请选择学校" clearable
								@change="changeSchoolNew">
								<el-option v-for="item in schools" :label="item.schoolname" :key="item.schoolid"
									:value="item.schoolid">
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="用户类型">
							<el-select v-model="queryParams.usertype" placeholder="请选择用户类型" clearable>
								<el-option v-for="dict in dict.type.bbs_user_type" :key="dict.value" :label="dict.label"
									:value="dict.value">
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="用户状态">
							<el-select v-model="queryParams.userstate" placeholder="请选择用户状态" clearable>
								<el-option v-for="dict in dict.type.bbs_user_state" :key="dict.value"
									:label="dict.label" :value="dict.value" />
							</el-select>
						</el-form-item>
						<el-form-item label="用户Id" prop="userid">
							<el-input v-model="queryParams.userid" placeholder="请输入用户Id" clearable
								@keyup.enter.native="handleQuery" />
						</el-form-item>
						<el-form-item label="用户昵称" prop="nickname">
							<el-input v-model="queryParams.nickname" placeholder="请输入用户昵称" clearable
								@keyup.enter.native="handleQuery" />
						</el-form-item>
						<el-form-item>
							<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
							</el-button>
							<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
						</el-form-item>
					</el-form>

					<el-row :gutter="10" class="mb8">
						<!-- 						<el-col :span="1.5">
							<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
								v-hasPermi="['bbs_user:bbs_user:add']">新增</el-button>
						</el-col>
						<el-col :span="1.5">
							<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
								@click="handleUpdate" v-hasPermi="['bbs_user:bbs_user:edit']">修改</el-button>
						</el-col>
						<el-col :span="1.5">
							<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
								@click="handleDelete" v-hasPermi="['bbs_user:bbs_user:remove']">删除</el-button>
						</el-col> -->
						<!-- 						<el-col :span="1.5">
							<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
								v-hasPermi="['bbs_user:bbs_user:export']">导出</el-button>
						</el-col> -->
						<el-col :span="1.5" v-if="selectionList && selectionList.length>1">
							<el-button type="primary" plain icon="el-icon-edit" size="mini"
								@click="handleAllJudge">全部认证通过</el-button>
						</el-col>
						<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
					</el-row>

					<el-table v-loading="loading" :data="bbs_userList" @selection-change="handleSelectionChange">
						<el-table-column type="selection" width="55" align="center" />
						<!-- 	<el-table-column label="唯一id" align="center" prop="userid" /> -->
						<el-table-column label="昵称" align="center" prop="nickname" width="120" />
						<el-table-column v-if="currentInAllSchool" label="学校" align="center" prop="schoolid" width="120"
							:formatter="formatSchoolName" />
						<el-table-column label="头像" align="center" prop="headimgurl">
							<template slot-scope="scope">
								<image-preview :src="scope.row.headimgurl" :width="50" :height="50" />
							</template>
							<!-- 							<template slot-scope="scope">
								　　　　<img :src="scope.row.headimgurl" width="50" height="50" class="head_pic" />
								　　</template> -->
						</el-table-column>
						<!-- <el-table-column label="手机号" align="center" prop="phone" width="120"
							v-if="$store.state.user.name.includes('admin') && isRoleIncludeAdmin()" /> -->
						<el-table-column label="手机号" align="center" prop="phone" width="120"
							v-if="isRoleIncludeAdmin()" />
						<!-- <el-table-column label="查看手机号" align="center" class-name="small-padding fixed-width">
							<template slot-scope="scope">
								<el-button size="mini" type="text" icon="el-icon-edit"
									@click="getphone(scope.row.phone)">查看电话
								</el-button>
							</template>
						</el-table-column> -->
						<el-table-column label="性别" align="center" prop="sex">
							<template slot-scope="scope">
								<dict-tag :options="dict.type.bbs_user_sex" :value="scope.row.sex" />
							</template>
						</el-table-column>
						<!-- <el-table-column label="简介" align="center" prop="introduction" /> -->
						<el-table-column label="用户id" width="150" align="center" prop="userid" />
						<!-- 						<el-table-column label="邮箱" align="center" prop="email" /> -->
						<el-table-column label="身份证明" align="center" prop="idcardurl">
							<template slot-scope="scope">
								<image-preview :src="scope.row.idcardurl" :width="50" :height="50" />
							</template>
						</el-table-column>
						<el-table-column label="用户类型" align="center" prop="usertype">
							<template slot-scope="scope">
								<dict-tag :options="dict.type.bbs_user_type" :value="scope.row.usertype" />
							</template>
						</el-table-column>
						<el-table-column label="用户状态" align="center" prop="userstate">
							<template slot-scope="scope">
								<dict-tag :options="dict.type.bbs_user_state" :value="scope.row.userstate" />
							</template>
						</el-table-column>
						<el-table-column label="查看发帖记录" align="center" class-name="small-padding fixed-width">
							<template slot-scope="scope">
								<el-button size="mini" type="text" icon="el-icon-edit"
									@click="toContentPage(scope.row)">查看列表
								</el-button>
							</template>
						</el-table-column>
						<el-table-column label="查看评论记录" align="center" class-name="small-padding fixed-width">
							<template slot-scope="scope">
								<el-button size="mini" type="text" icon="el-icon-edit"
									@click="toCommentPage(scope.row)">查看评论列表
								</el-button>
							</template>
						</el-table-column>
						<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
							<template slot-scope="scope">
								<el-button v-if="scope.row.userstate!==1" size=" mini" type="text" icon="el-icon-edit"
									@click="handleUpdate(scope.row)">修改
								</el-button>
								<el-button v-if="scope.row.userstate===1" size="mini" type="text" icon="el-icon-edit"
									@click="handleJudge(scope.row, 2)">通过
								</el-button>
								<el-button v-if="scope.row.userstate===1" size="mini" type="text" icon="el-icon-delete"
									@click="handleJudge(scope.row, 0)">拒绝
								</el-button>
							</template>
						</el-table-column>
						<el-table-column label="上次登陆时间" align="center" prop="createTime" width="150">
							<template slot-scope="scope">
								<span>{{ parseTime(scope.row.lastlogintime) }}</span>
							</template>
						</el-table-column>
						<!-- <el-table-column label="openId" align="center" prop="openid"
							v-if="$store.state.user.name=='admin'" width="150"/> -->
						<el-table-column label="openId" align="center" prop="openid" v-if="isRoleIncludeAdmin()"
							width="150" />
						<el-table-column label="官方认证" align="center" class-name="small-padding fixed-width">
							<template slot-scope="scope">
								<el-button size="mini" type="text" icon="el-icon-edit"
									@click="changeUserState(scope.row,10)">改为官方认证
								</el-button>
								<el-button size="mini" type="text" icon="el-icon-delete"
									@click="changeUserState(scope.row,0)">改为普通用户
								</el-button>
							</template>
						</el-table-column>
					</el-table>

					<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
						:limit.sync="queryParams.pageSize" @pagination="getNextPage()" />

					<!-- 添加或修改论坛用户对话框 -->
					<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="cancel">
						<el-form ref="form" :model="form" :rules="rules" label-width="80px">

							<userUpdateForm v-if="showUserUpdateForm" ref="userUpdateForm"
								:currentUserId="targetRow.userid" :currentSchoolId="targetRow.schoolid">
							</userUpdateForm>

						</el-form>
						<div slot="footer" class="dialog-footer">
							<el-button type="primary" @click="submitUserUpdateForm">确 定</el-button>
							<el-button @click="cancel">取 消</el-button>
						</div>
					</el-dialog>
					<el-dialog title="用户发帖记录" :visible.sync="contentListOpen" width="80%" append-to-body>
						<el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange"
							:row-style="{background:'white'}">
							<el-table-column type="selection" width="55" align="center" />
							<el-table-column label="发帖人" align="center">
								<template slot-scope="scope">
									<el-button size="mini" type="text" @click="toUserPage(scope.row)">
										{{scope.row.nickname}}
									</el-button>
								</template>
							</el-table-column>
							<el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true"
								width="150" />
							<el-table-column label="内容" align="center" prop="contenttext" :show-overflow-tooltip="true"
								width="200" />
							<el-table-column label="点赞数" align="center" prop="upnum" width="60" />
							<el-table-column label="评论数" align="center" prop="commentnum" width="60" />
							<el-table-column label="阅读人数" align="center" prop="realreadnum" width="80" />
							<el-table-column label="阅读次数" align="center" prop="readnum" width="80" />
							<el-table-column label="类型" align="center" prop="contenttype">
								<template slot-scope="scope">
									<dict-tag :options="dict.type.bbs_new_content_type"
										:value="scope.row.contenttype" />
								</template>
							</el-table-column>
							<el-table-column label="特殊类型" align="center" prop="isspecial" width="90">
								<template slot-scope="scope">
									<dict-tag :options="dict.type.bbs_content_special" :value="scope.row.isspecial" />
								</template>
							</el-table-column>
							<el-table-column label="帖子状态" align="center" prop="contentstate">
								<template slot-scope="scope">
									<dict-tag :options="dict.type.bbs_content_state" :value="scope.row.contentstate" />
								</template>
							</el-table-column>
							<!-- 							<el-table-column label="详情" align="center" class-name="small-padding fixed-width">
								<template slot-scope="scope">
									<el-button size="mini" type="text" icon="el-icon-edit" @click="openModal(scope.row)">查看详情
									</el-button>
								</template>
							</el-table-column>
							<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
								<template slot-scope="scope">
									<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改
									</el-button>

								</template>
							</el-table-column> -->
							<el-table-column label="创建时间" align="center" prop="createtime" width="180">
								<template slot-scope="scope">
									<span>{{ parseTime(scope.row.createtime) }}</span>
								</template>
							</el-table-column>
							</el-table-column>
							<el-table-column label="可见范围" align="center" prop="visiblerange">
								<template slot-scope="scope">
									<dict-tag :options="dict.type.bbs_content_visibleRange"
										:value="scope.row.visiblerange" />
								</template>
							</el-table-column>


						</el-table>
						<pagination v-show="contentTotal>0" :total="contentTotal"
							:page.sync="contentQueryParams.pageNum" :limit.sync="contentQueryParams.pageSize"
							@pagination="getContentList()" />
					</el-dialog>
				</div>
			</el-main>
		</el-container>
	</div>
</template>

<script>
	import {
		listBbs_user,
		getBbs_user,
		delBbs_user,
		addBbs_user,
		updateBbs_user,
		getAllSchools,
		judgeBbs_user,
		sendSubscribeMessage,
		addRewardScore,
		changeUserState,
		allSchoolUserList
	} from "@/api/bbs_user/bbs_user";
	import {
		listContentVo,
	} from "@/api/bbs_content/content";
	import {
		schoolInfo
	} from '@/utils/currentSchool';
	import {
		addBbs_operation_record,
	} from "@/api/bbs_operation_record/bbs_operation_record";
	import schoolInfoList from '../../../utils/schoolInfo.js';
	import {
		encrypt,
		decrypt
	} from '@/utils/rsa';
	import userUpdateForm from "../../components/userInfo/updateForm.vue"
	export default {
		name: "Bbs_user",
		dicts: ['bbs_user_state', 'bbs_user_type', 'bbs_user_sex', 'bbs_content_state', 'bbs_content_type',
			'bbs_content_visibleRange', 'bbs_new_content_type', 'bbs_content_special'
		],
		components: {
			userUpdateForm
		},
		data() {
			return {
				//学校列表
				schools: "",

				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 论坛用户表格数据
				bbs_userList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				//查询参数
				schoolObj: {
					pageNum: 1,
					pageSize: 2,
					schoolId: null,
					schoolName: null,
					schoolType: null,
					schoolAddress: null,
					plateNum: 0,
					userNum: 0,
					contentNum: 0,
					state: null,
					userState: null,
					userType: null,
				},
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					// schoolid: schoolInfo.schoolid,
					schoolid: null,
					userid: null,
					// phone: null,
					// sex: null,
					// email: null,
					// idcardurl: null,
					// nickname: null,
					// headimgurl: null,
					// platenum: null,
					usertype: null,
					userstate: null,
					orderByColumn: 'updatetime',
					// lastip: null,
					// introduction: null,
				},

				updateUserObj: {
					userid: null,
					userstate: null,
					usertype: null
				},
				userParams: "",
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				//图片预览列表
				imgList: [],
				// 帖子列表查询参数
				contentQueryParams: {
					pageNum: 1,
					pageSize: 10,
					userid: null,
					plateid: null,
					schoolid: null,
					title: null,
					contenttext: null,
					contenturl: null,
					place: null,
					upnum: null,
					downnum: null,
					commentnum: null,
					forwardnum: null,
					realreadnum: null,
					readnum: null,
					informnum: null,
					isspecial: null,
					visiblerange: null,
					contentstate: null,
					contenttype: null,
					voteid: null,
					createtime: null,
					updatetime: null,
					orderByColumn: 'createtime'
				},
				contentList: [],
				contentTotal: 0,
				contentListOpen: false,
				//操作记录有关
				userOldForm: {},
				operatinForm: {
					// id: null,
					schoolId: null,
					recordState: 1,
					operator: null,
					targetKind: null,
					targetId: null,
					changeInfo: null,
					note: null,
					// operateTime: null,
					bannedTime: null,
					// createTime: null,
					// updateTime: null
				},
				pageCreate: 0,
				showUserUpdateForm: false,
				targetRow: {},
				currentInAllSchool: false,
				selectionList: [],

			};
		},
		created() {
			this.getSchools();
			if (!this.$route.query.id) {
				// console.log("页面创建")
				this.pageCreate = 1
				this.queryParams.schoolid = this.$store.state.user.currentSchool.schoolid
				//将上次带条件跳转时的条件清空
				this.queryParams.userid = null;
				this.getList();
			}
			// console.log('当前用户',this.$store.state.user.name,this.$store.state.user.roles)
		},
		activated: function() {
			if (this.$route.query.id) {
				console.log('参数', this.$route.query.id)
				this.queryParams.schoolid = this.$store.state.user.currentSchool.schoolid
				this.queryParams.userid = this.$route.query.id
				this.getList();
				// this.queryParams.userid = null
			} else if (!this.$route.query.id) {
				if (this.pageCreate == 1) {
					return
				}
				// console.log("出发")
				this.queryParams.userid = null;
				this.queryParams.schoolid = this.$store.state.user.currentSchool.schoolid
				this.getList();
			}
		},
		beforeRouteLeave(to, from, next) {
			console.log('离开页面时调用的方法');
			// 清空条件
			this.queryParams.userid = null;
			this.queryParams.nickname = null;
			this.queryParams.usertype = null;
			this.queryParams.userstate = null;
			// 清除当前页面的路由参数
			// const data =this.$route
			// console.log('当前路由',data)
			if (this.$route.query.id) {
				delete this.$route.query.id
			}
			next();
		},
		methods: {
			/*查询学校列表*/
			getSchools() {
				// this.getAllSchools();
				// alert(111);
				//console.log("测试了");
				getAllSchools().then((res) => {
					// console.log(111);
					this.schools = res.data;
					console.log("呀哈哈", res.data);
					// //全部学校
					let schoolObj = {
						schoolid: [],
						schoolname: "全部"
					};
					for (let i = 0; i < res.data.length; i++) {
						schoolObj.schoolid.push(res.data[i].schoolid)
					}
					schoolObj.schoolid = schoolObj.schoolid.join(',');
					this.schools.unshift(schoolObj);
				});
			},
			/** 查询论坛用户列表 */
			getList() {
				this.loading = true;
				console.log("哈哈哈哈", this.$store.state.user.currentSchool)
				this.queryParams.schoolid = this.$store.state.user.currentSchool.schoolid
				if (!this.queryParams.schoolid) {
					this.$message.error('请选择学校');
					return
				}
				console.log(this.queryParams)
				listBbs_user(this.queryParams).then((response) => {
					console.log(response);
					this.bbs_userList = response.rows;
					this.total = response.total;
					this.loading = false;
					this.imgList = this.bbs_userList[0].idcardurl;
					//处理加密数据
					for (let i = 0; i < this.bbs_userList.length; i++) {

						if (String(this.bbs_userList[i].phone).length > 500) {
							this.bbs_userList[i].phone = this.getDecryptedData(this.bbs_userList[i].phone)
						}
						if (String(this.bbs_userList[i].idcardurl).length > 500) {
							this.bbs_userList[i].idcardurl = this.getDecryptedData(this.bbs_userList[i].idcardurl)
							let imgArr = this.bbs_userList[i].idcardurl.split("\"")
							if (imgArr && imgArr.length > 2) {
								console.log(imgArr)
								this.bbs_userList[i].idcardurl = imgArr[1]
							}
							// this.bbs_userList[i].idcardurl.replace("\"","")
						}
					}
					console.log(this.bbs_userList);
					this.pagecreate = 0;
				});
			},
			getNextPage() {
				if (this.currentInAllSchool) {
					this.getAllSchoolUserList(this.queryParams.schoolid)
				} else {
					this.getList()
				}
			},
			// 取消按钮
			cancel() {
				this.showUserUpdateForm = false;
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					userid: null,
					phone: null,
					password: null,
					sex: null,
					stunum: null,
					city: null,
					email: null,
					idcardurl: null,
					nickname: null,
					headimgurl: null,
					platenum: null,
					openid: null,
					unionid: null,
					usertype: null,
					userstate: null,
					lastip: null,
					introduction: null,
					createtime: null,
					updatetime: null,
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				if (this.currentInAllSchool) {
					this.getAllSchoolUserList(this.queryParams.schoolid)
				} else {
					this.getList();
				}

			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				console.log('选择', selection)
				// this.ids = selection.map((item) => item.userid);
				// this.single = selection.length !== 1;
				// this.multiple = !selection.length;
				this.selectionList = selection
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加论坛用户";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				this.targetRow = row;
				this.showUserUpdateForm = true
				console.log("row", row);
				const userid = row.userid || this.ids;
				getBbs_user(userid).then((response) => {
					this.form = response.data;
					//字典需要处理为字符型
					//这里需要改成row的ids
					// this.form.userstate = this.form.userstate.toString()
					// this.form.usertype = this.form.usertype.toString()
					this.form.userstate = row.userstate.toString();
					this.form.usertype = row.usertype.toString()
					console.log(this.form)
					//将此时的form复制一份，存起来
					this.userOldForm = JSON.parse(JSON.stringify(this.form));
					this.operatinForm.note = null;
					this.operatinForm.bannedTime = null;
					this.open = true;
					this.title = "修改论坛用户";
				});
			},
			handleJudge(row, res) {
				let year = new Date().getFullYear();
				let month = new Date().getMonth() + 1;
				let day = new Date().getDate();
				let time = year + '年' + month + '月' + day + '日'
				console.log('触发远程接口', row, time)
				//初始化模板消息内容
				let phrase6String = ""
				if (res == 2) {
					phrase6String = "认证成功"
				} else {
					phrase6String = "认证失败"
				}
				let templateData = {
					"thing2": {
						"value": "校友身份认证"
					},
					"phrase6": {
						"value": phrase6String
					},
					"time8": {
						"value": time
					}
				}
				//接口请求参数
				let sendMessageParams = {
					"data": templateData,
					"openid": row.openid,
					"source": "BBSadmin",
					// "template_id": schoolInfo.templateId,
					"template_id": this.$store.state.user.currentSchool.templateId,
					"APPID": this.$store.state.user.currentSchool.APPID
				}
				let rewardScoreParams = {
					"userId": row.userid,
					"source": "BBSadmin",
					"changeType": 5,
					"changeNum": 200
				}

				//修改数据库
				row.userstate = res;
				const judgeObj = {};
				judgeObj.userid = row.userid;
				judgeObj.state = row.userstate;
				judgeObj.schoolid = this.queryParams.schoolid;
				if (this.currentInAllSchool) {
					judgeObj.schoolid = row.schoolid;
				}
				if (this.currentInAllSchool && row.schoolid && (!sendMessageParams.APPID || !sendMessageParams
						.template_id || this.$store.state.user.currentSchool.schoolid != row.schoolid)) {
					const schoolRecord = schoolInfoList.schoolInfoList.find(school => school.schoolid === row.schoolid);
					if (schoolRecord) {
						console.log("触发")
						sendMessageParams.APPID = schoolRecord.APPID;
						sendMessageParams.template_id = schoolRecord.templateId;
					} else {
						console.log("无信息", row.schoolid, schoolInfoList.schoolInfoList);
						return;
					}
				}
				judgeBbs_user(judgeObj).then((res) => {
					//发送通知
					sendSubscribeMessage(sendMessageParams).then((res) => {
						console.log(res)
						if (res.data.errcode == 0) {
							this.$modal.msgSuccess("发送通知成功");
						} else {
							this.$modal.msgError(res.data.errmsg);
						}
					});
					//增加认证积分
					addRewardScore(rewardScoreParams).then((res) => {
						console.log(res)
						if (res.code == 200) {
							this.$modal.msgSuccess("认证积分增加成功");
						} else {
							this.$modal.msgError(res.data.errmsg);
						}
					});

					//刷新列表
					if (this.currentInAllSchool) {
						this.getAllSchoolUserList(this.queryParams.schoolid)
					} else {
						this.getList();
					}
				});
				// judgeBbs_user(row)
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate((valid) => {
					if (valid) {
						if (this.form.userid != null) {
							//如果是从其他状态改为禁言状态，需要确保有禁言时间
							if (this.form.usertype == 2 && this.form.usertype != this.userOldForm.usertype && !this
								.operatinForm.bannedTime) {
								this.$message.error('请输入禁言时间');
								this.loading = false;
								return
								// this.operatinForm.bannedTime = null
							}
							this.updateUserObj.userid = this.form.userid;
							this.updateUserObj.userstate = this.form.userstate;
							this.updateUserObj.usertype = this.form.usertype;
							this.updateUserObj.schoolid = this.queryParams.schoolid;
							console.log("updateUserObj", this.updateUserObj);
							// updateBbs_user(this.form).then((response) => {
							// 	this.$modal.msgSuccess("修改成功");
							// 	this.open = false;
							// 	this.getList();
							// });
							updateBbs_user(this.updateUserObj).then((response) => {
								this.$modal.msgSuccess("修改成功");
								this.createOperationRecord(1, this.userOldForm, this.form)
								this.open = false;
								this.getList();
								this.updateUserObj = {
									userid: "",
									userType: "",
									userState: ""
								}
							});
						} else {
							addBbs_user(this.form).then((response) => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			//调用子组件
			submitUserUpdateForm() {
				//修改用户状态
				let code = this.$refs.userUpdateForm.updateUserInfo();
				//未填写禁言时间
				if (code == 0) {
					return
				}
				this.showUserUpdateForm = false;
				this.open = false;
			},
			//创建操作记录
			createOperationRecord(targetKind, oldInfo, newInfo) {
				console.log("触发")
				//构建操作记录
				this.operatinForm.schoolId = this.$store.state.user.currentSchool.schoolid;
				this.operatinForm.operator = this.$store.state.user.name;
				this.operatinForm.targetKind = targetKind;
				//操作对象为用户
				if (targetKind == 1) {
					this.operatinForm.targetId = newInfo.userid;
				}
				this.operatinForm.changeInfo = {
					data: {
						oldForm: {},
						newForm: {}
					}
				};
				//获取变更了属性值
				let haveChange = 0;
				for (let key in newInfo) {
					let value = newInfo[key];
					// 在这里对属性进行处理
					// console.log(key + ': ' + typeof newInfo[key]);
					if (typeof newInfo[key] != 'object' && newInfo[key] != oldInfo[key]) {
						haveChange = 1
						this.operatinForm.changeInfo.data.oldForm[key] = oldInfo[key]
						this.operatinForm.changeInfo.data.newForm[key] = newInfo[key]
					}
				}
				if (haveChange == 1) {
					//如果存在禁言时间
					if (this.operatinForm.bannedTime) {
						this.operatinForm.note = this.operatinForm.note + " 禁言" + this.operatinForm.bannedTime * 1 + "天"
						const bannedtime = new Date(new Date().getTime() + this.operatinForm.bannedTime * 24 * 60 * 60 *
							1000)
						this.operatinForm.bannedTime = this.formatDateTime(bannedtime)
						console.log(this.operatinForm.bannedTime)
					}
					this.operatinForm.changeInfo = JSON.stringify(this.operatinForm.changeInfo)
					console.log(this.operatinForm)
					addBbs_operation_record(this.operatinForm).then(response => {
						this.$modal.msgSuccess("操作记录新增成功");
					});
				}
			},
			formatDateTime(date) {
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				const hours = date.getHours().toString().padStart(2, '0');
				const minutes = date.getMinutes().toString().padStart(2, '0');
				const seconds = date.getSeconds().toString().padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const userids = row.userid || this.ids;
				this.$modal
					.confirm('是否确认删除论坛用户编号为"' + userids + '"的数据项？')
					.then(function() {
						return delBbs_user(userids);
					})
					.then(() => {
						this.getList();
						this.$modal.msgSuccess("删除成功");
					})
					.catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download(
					"bbs_user/bbs_user/export", {
						...this.queryParams,
					},
					`bbs_user_${new Date().getTime()}.xlsx`
				);
			},
			previewImg(img) {
				//console.log(123)
				this.imgList = [];
				this.imgList.push(img);
			},
			//跳转帖子界面
			toContentPage(data) {
				console.log('触发', data)
				this.$router.push({
					path: '/bbs_user_school/content',
					query: {
						userId: data.userid
					}
				})
			},
			//跳转评论界面
			toCommentPage(data) {
				console.log('触发', data)
				this.$router.push({
					path: '/bbs_user_school/comment',
					query: {
						userId: data.userid
					}
				})
			},
			//查询发帖记录
			openContentModal(row) {
				this.contentQueryParams.schoolid = this.$store.state.user.currentSchool.schoolid
				this.contentQueryParams.userid = row.userid
				this.getContentList();
				this.contentListOpen = true;
			},
			getContentList() {
				this.loading = true;
				listContentVo(this.contentQueryParams).then(response => {
					this.contentList = response.rows;
					for (var i = 0; i < this.contentList.length; i++) {
						//兼容旧版特殊类型
						if (this.contentList[i].contenttype == 2) {
							this.contentList[i].isspecial = 3
						} else if (this.contentList[i].contenttype == 3) {
							this.contentList[i].isspecial = 1
						} else if (this.contentList[i].contenttype == 4) {
							this.contentList[i].isspecial = 2
						} else {

						}
						//兼容旧版类型
						if (this.contentList[i].contenttype < 5 && this.contentList[i].contenttype != 1) {
							this.contentList[i].contenttype = 5
						} else {

						}

						if (this.contentList[i].contenturl) {
							this.contentList[i].contenturl = this.contentList[i].contenturl.split(',')
						} else {
							this.contentList[i].contenturl = []
						}

					}
					console.log('内容', this.contentList)
					this.contentTotal = response.total;
					this.loading = false;
				});
			},

			changeSchoolNew(value) {
				console.log("改变", value)
				if (typeof value === 'string' && value.includes(',')) {
					console.log("数组")
					this.currentInAllSchool = true
					this.getAllSchoolUserList(value);
				} else {
					this.currentInAllSchool = false
					this.currentschools = schoolInfoList.selectOneSchoolInfo(value)
					this.$store.commit('SET_currentSchool', this.currentschools)
					console.log("当前学校是", this.currentschools, "store", this.$store.state.user.currentSchool)
					this.getList();
				}
			},
			getDecryptedData(encryptedData) {
				let decryptedData = decrypt(encryptedData)
				console.log(decryptedData)
				return decryptedData
			},
			changeUserState(row, userState) {
				let userOldForm = {
					userstate: 0
				}
				if (userState != 10) {
					userOldForm.userstate = 10
				}
				let form = {
					userid: row.userid,
					userstate: userState
				}
				changeUserState(form).then((response) => {
					this.$modal.msgSuccess("官方认证状态修改成功");
					this.createOperationRecord(1, userOldForm, form)
				});
			},
			/** 查询论坛用户列表 */
			getAllSchoolUserList(value) {
				this.loading = true;
				this.queryParams.schoolid = value
				let queryParams = JSON.parse(JSON.stringify(this.queryParams))
				queryParams.schoolid = null;
				queryParams.schoolIdStr = value
				allSchoolUserList(queryParams).then((response) => {
					console.log(response);
					this.bbs_userList = response.rows;
					this.total = response.total;
					this.loading = false;
					this.imgList = this.bbs_userList[0].idcardurl;
					//处理加密数据
					for (let i = 0; i < this.bbs_userList.length; i++) {
						if (!this.bbs_userList[i].idcardurl) {
							this.bbs_userList[i].idcardurl = ''
						}
						if (String(this.bbs_userList[i].phone).length > 500) {
							this.bbs_userList[i].phone = this.getDecryptedData(this.bbs_userList[i].phone)
						}
						if (String(this.bbs_userList[i].idcardurl).length > 500) {
							this.bbs_userList[i].idcardurl = this.getDecryptedData(this.bbs_userList[i].idcardurl)
							if (this.bbs_userList[i].idcardurl) {
								let imgArr = this.bbs_userList[i].idcardurl.split("\"")
								if (imgArr && imgArr.length > 2) {
									console.log(imgArr)
									this.bbs_userList[i].idcardurl = imgArr[1]
								}
							}
							// this.bbs_userList[i].idcardurl.replace("\"","")
						}
					}
					console.log(this.bbs_userList);
					this.pagecreate = 0;
				});
			},
			formatSchoolName(row, column, cellValue) {
				const school = this.schools.find(s => s.schoolid === cellValue);
				return school ? school.schoolname : '';
			},
			isRoleIncludeAdmin() {
				// console.log('呀哈哈',this.$store.state.user)
				let haveAdmin = false
				if (this.$store.state.user.roles && this.$store.state.user.roles.length > 0) {
					for (let i = 0; i < this.$store.state.user.roles.length; i++) {
						if (this.$store.state.user.roles[i].includes('admin')) {
							haveAdmin = true
							console.log(this.$store.state.user.roles[i])
							break
						}
					}
				}
				return haveAdmin
			},
			async handleAllJudge() {
				console.log(this.selectionList)
				let that = this
				for (let i = 0; i < this.selectionList.length; i++) {
					let year = new Date().getFullYear();
					let month = new Date().getMonth() + 1;
					let day = new Date().getDate();
					let time = year + '年' + month + '月' + day + '日';
					console.log('触发远程接口', time);

					// 初始化模板消息内容
					let phrase6String = "认证成功";

					let templateData = {
						"thing2": {
							"value": "校友身份认证"
						},
						"phrase6": {
							"value": phrase6String
						},
						"time8": {
							"value": time
						}
					};

					// 接口请求参数
					let sendMessageParams = {
						"data": templateData,
						"openid": this.selectionList[i].openid,
						"source": "BBSadmin",
						"template_id": this.$store.state.user.currentSchool.templateId,
						"APPID": this.$store.state.user.currentSchool.APPID
					};

					let rewardScoreParams = {
						"userId": this.selectionList[i].userid,
						"source": "BBSadmin",
						"changeType": 5,
						"changeNum": 200
					};

					// 修改数据库
					this.selectionList[i].userstate = 2;
					const judgeObj = {
						userid: this.selectionList[i].userid,
						state: this.selectionList[i].userstate,
						schoolid: this.queryParams.schoolid
					};

					if (this.currentInAllSchool) {
						judgeObj.schoolid = this.selectionList[i].schoolid;
					}

					if (this.currentInAllSchool && this.selectionList[i].schoolid &&
						(!sendMessageParams.APPID || !sendMessageParams.template_id ||
							this.$store.state.user.currentSchool.schoolid != this.selectionList[i].schoolid)) {
						const schoolRecord = schoolInfoList.schoolInfoList.find(school => school.schoolid === this
							.selectionList[i].schoolid);
						if (schoolRecord) {
							console.log("触发");
							sendMessageParams.APPID = schoolRecord.APPID;
							sendMessageParams.template_id = schoolRecord.templateId;
						} else {
							console.log("无信息", this.selectionList[i].schoolid, schoolInfoList.schoolInfoList);
							return;
						}
					}

					try {
						const judgeRes = await judgeBbs_user(judgeObj);

						// 发送通知
						const sendMessageRes = await sendSubscribeMessage(sendMessageParams);
						if (sendMessageRes.data.errcode == 0) {
							this.$modal.msgSuccess("发送通知成功");
						} else {
							this.$modal.msgError(sendMessageRes.data.errmsg);
						}

						// 增加认证积分
						const rewardScoreRes = await addRewardScore(rewardScoreParams);
						if (rewardScoreRes.code == 200) {
							this.$modal.msgSuccess("认证积分增加成功");
						} else {
							this.$modal.msgError(rewardScoreRes.data.errmsg);
						}

						// 刷新列表
					} catch (error) {
						console.error('操作失败:', error);
					}
				}
				if (this.currentInAllSchool) {
					this.getAllSchoolUserList(this.queryParams.schoolid)
				} else {
					this.getList();
				}
			}

		},
	};
</script>